package uDdf;

/**
 * Created with IntelliJ IDEA
 * Description:基于CAS的自旋锁实现
 * User:34398
 * Date:2024-07-30
 * Time:10:20
 */


class spinLock {

    private boolean CAS(Thread address, Thread register1, Thread register2) {
        if (address == register1) {
            address = register2;
            return true;
        }
        return false;
    }


    private Thread owner = null;


    /**
     * 加锁操作
     */
    public void lock() {
        while (!CAS(this.owner, null, Thread.currentThread())) {
            //空循环
        }
        //CAS为true才会出循环，也就是已经拿到所锁了
    }

    /**解锁操作*/
    public void unlock(){
        this.owner=null;//直接置为空即可
    }

}




public class Test1 {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer();
        sb.append('a');
        sb.append('b');
        sb.append('c');
        sb.append('d');
    }
}
